package cn.patpang.core.tool;

import org.apache.commons.lang3.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Title: ValidateTool.java
 * @Author Patrick Pang
 * @Date 2015-3-11
 * @Description 验证数据
 */
public class ValidateTool {

    /**
     * 验证邮箱
     *
     * @param email
     * @return
     */
    public static boolean isEmail(String email) {
        boolean flag = false;
        try {
            if (StringUtils.isNotBlank(email) && StringUtils.isNotBlank(email = email.trim())) {
                String check = "^([a-z0-9A-Z]+[-|_|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
                Pattern regex = Pattern.compile(check);
                Matcher matcher = regex.matcher(email);
                flag = matcher.matches();
            }
        } catch (Exception e) {
            flag = false;
        }
        return flag;
    }

    /**
     * 验证邮箱
     *
     * @param emails
     * @return
     */
    public static boolean isEmail(String[] emails) {
        if (null != emails && emails.length > 0) {
            for (String email : emails) {
                if (!isEmail(email)) {
                    return false;
                }
            }

            return true;
        }

        return false;
    }

    /**
     * 验证手机号码
     *
     * @param telphone
     * @return
     */
    public static boolean isMobile(String telphone) {
        boolean flag = false;
        try {
            Pattern regex = Pattern.compile("^0?1[3|4|5|7|8][0-9]\\d{8}$");
            Matcher matcher = regex.matcher(telphone);
            flag = matcher.matches();
        } catch (Exception e) {
            flag = false;
        }
        return flag;
    }

    /**
     * 验证QQ
     *
     * @param qq
     * @return
     */
    public static boolean isQQ(String qq) {
        boolean flag = false;
        try {
            Pattern regex = Pattern.compile("^[1-9][0-9]{4,13}$");
            Matcher matcher = regex.matcher(qq);
            flag = matcher.matches();
        } catch (Exception e) {
            flag = false;
        }
        return flag;
    }

    /**
     * 判断整个数组是否有空值
     *
     * @param params
     * @return
     */
    public static boolean hasEmpty(String[] params) {
        if (null != params && params.length > 0) {
            for (String param : params) {
                if (null == param || StringUtils.isBlank(param.trim())) {
                    return false;
                }
            }

            return true;
        }

        return false;
    }

    public static boolean isNumeric(String str) {
        for (int i = str.length(); --i >= 0; ) {
            int chr = str.charAt(i);
            if (chr < 48 || chr > 57)
                return false;
        }
        return true;
    }

    public static boolean isNotNZ(Integer i) {
        return (null != i && i > 0);
    }

    /**
     * 判断字符串是否是整数
     */
    public static boolean isInteger(String value) {
        try {
            Integer.parseInt(value);
            return true;
        } catch (NumberFormatException e) {
            return false;
        }
    }

    /**
     * 判断字符串是否是浮点数
     */
    public static boolean isDouble(String value) {
        try {
            Double.parseDouble(value);
            if (value.contains("."))
                return true;
            return false;
        } catch (NumberFormatException e) {
            return false;
        }
    }

    public static boolean isNum(String str) {
        return str.matches("^[-+]?(([0-9]+)([.]([0-9]+))?|([.]([0-9]+))?)$");
    }


    /**
     * 验证integer 为空等于0
     *
     * @param eqZero
     * @param is
     * @return
     */
    public static boolean emptyInt(boolean eqZero, Integer... is) {
        if (null != is && is.length > 0) {
            for (Integer i : is) {
                if (null == i || (eqZero && i == 0)) {
                    return true;
                }
            }
        }

        return false;
    }
}
